package com.afkt.project.ui.slice;

import com.afkt.project.base.app.BaseActivity;
import com.afkt.project.util.LogUtils;
import com.afkt.project.util.ProjectUtils;
import com.afkt.project.util.ToastUtil;
import com.dev.utils.LogPrintUtils;
import com.dev.utils.app.StringUtils;
import com.dev.widget.qrcode.core.ZbarView;
import com.dev.widget.ui.ScanShapeView;
import com.ohos.afkt.ResourceTable;
import com.dev.base.widget.BaseImageView;
import com.dev.utils.app.ListenerUtils;
import com.dev.utils.app.ViewUtils;
import ohos.aafwk.content.Intent;
import ohos.agp.components.*;
import ohos.agp.components.surfaceprovider.SurfaceProvider;
import ohos.agp.window.dialog.ToastDialog;
import ohos.app.Context;
import ohos.media.image.ImageReceiver;
import ohos.utils.IntentConstants;
import ohos.vibrator.agent.VibratorAgent;

import java.util.ArrayList;
import java.util.EnumMap;
import java.util.List;
import java.util.Map;

public class ScanShapeActivity extends BaseActivity implements Component.ClickedListener, ZbarView.OnZbarCameraScanner {

    private static final String TAG = "ScanShapeActivity";

    //标题栏
    private DirectionalLayout bar_layout;
    private DirectionalLayout bar;
    private Image qr_return;
    //页面按钮
    private BaseImageView vidAssFlashlightIgview;
    private BaseImageView vidAssSquareIgview;
    private BaseImageView vidAssHexagonIgview;
    private BaseImageView vidAssAnnulusIgview;
    private BaseImageView vidAssImageIgview;
    private SurfaceProvider surfaceProvider;
    private ImageReceiver imageReceiver;
    private ScanShapeView scanShapeView;
    private ZbarView zbarView;
    private int startLine=0;


    @Override
    protected int baseLayoutId() {
        return ResourceTable.Layout_ability_qrcodescam;
    }

    @Override
    protected void initView() {
        getWindow().setTransparent(true);
        //标题栏
        bar_layout = (DirectionalLayout) findComponentById(ResourceTable.Id_qr_barLayout);
        bar = (DirectionalLayout) LayoutScatter.getInstance(this).parse(ResourceTable.Layout_scan_bar, null, false);
        bar_layout.addComponent(bar);
        qr_return = (Image) bar.findComponentById(ResourceTable.Id_bar_return);
        //页面按钮
        vidAssImageIgview = (BaseImageView) findComponentById(ResourceTable.Id_vid_ass_image_igview);
        vidAssImageIgview.setVisibility(Component.HIDE);
        vidAssFlashlightIgview = (BaseImageView) findComponentById(ResourceTable.Id_vid_ass_flashlight_igview);
        vidAssSquareIgview = (BaseImageView) findComponentById(ResourceTable.Id_vid_ass_square_igview);
        vidAssHexagonIgview = (BaseImageView) findComponentById(ResourceTable.Id_vid_ass_hexagon_igview);
        vidAssAnnulusIgview = (BaseImageView) findComponentById(ResourceTable.Id_vid_ass_annulus_igview);
        surfaceProvider=(SurfaceProvider) findComponentById(ResourceTable.Id_zbar_view);
        scanShapeView=(ScanShapeView)findComponentById(ResourceTable.Id_scanShapeView);
        zbarView=new ZbarView(this,surfaceProvider);
         // 设置扫描类型
        ProjectUtils.refShape(scanShapeView, ScanShapeView.Shape.Square);

    }


    @Override
    protected void onActive() {
        super.onActive();
        if(startLine>0){
            zbarView=new ZbarView(this,surfaceProvider);
            zbarView.openCamera();
            surfaceProvider.invalidate();
        }
        startLine++;
        scanShapeView.startAnim();
        zbarView.setCameraScanner(this);
    }



    @Override
    protected void initListener() {

        ListenerUtils.setOnClicks(this, vidAssFlashlightIgview,
                vidAssSquareIgview, vidAssHexagonIgview, vidAssAnnulusIgview,
                vidAssImageIgview);

        qr_return.setClickedListener(component -> onBackPressed());
    }

    @Override
    public void onClick(Component component) {
        switch (component.getId()) {
            case ResourceTable.Id_vid_ass_flashlight_igview:
                // 设置开关
                setFlashlight(!ViewUtils.isSelected(vidAssFlashlightIgview));
                break;
            case ResourceTable.Id_vid_ass_square_igview:
                ProjectUtils.refShape(scanShapeView, ScanShapeView.Shape.Square);
                break;
            case ResourceTable.Id_vid_ass_hexagon_igview:
                ProjectUtils.refShape(scanShapeView, ScanShapeView.Shape.Hexagon);
                break;
            case ResourceTable.Id_vid_ass_annulus_igview:
                ProjectUtils.refShape(scanShapeView, ScanShapeView.Shape.Annulus);
                break;
            case ResourceTable.Id_vid_ass_image_igview:
              /*  // 初始化图片配置
                PictureSelectorUtils.PicConfig picConfig = new PictureSelectorUtils.PicConfig()
                        .setCompress(false).setMaxSelectNum(1).setCrop(false).setMimeType(PictureMimeType.ofImage())
                        .setCamera(true).setGif(false);
                // 打开图片选择器
                PictureSelectorUtils.openGallery(PictureSelector.create(this), picConfig);*/
                Intent intent = new Intent();
                intent.setAction(Intent.ACTION_PLAY);
                intent.setType(IntentConstants.ACTION_HOME);
                startAbility(intent);
                break;
        }
    }
    /**
     * 设置手电筒开关
     *
     * @param open 是否打开
     */
    private void setFlashlight(boolean open) {
        if (open) {
            zbarView.openFlashlight(); // 打开闪光灯
            vidAssFlashlightIgview.setPixelMap(ResourceTable.Media_icon_flashlight_on);
        } else {
            zbarView.closeFlashlight(); // 关闭闪光灯
            vidAssFlashlightIgview.setPixelMap(ResourceTable.Media_icon_flashlight_off);
        }
        ViewUtils.setSelected(open, vidAssFlashlightIgview);
    }

    @Override
    protected void onStop() {
        super.onStop();

    }

    @Override
    protected void onBackground() {
        // 停止动画
        scanShapeView.stopAnim();
        zbarView.stopCamera(); // 关闭摄像头预览，并且隐藏扫描框
        bar_layout.removeComponent(bar);
        getWindow().setTransparent(false);
        LogUtils.error(TAG, "isPresent");
        super.onBackground();
    }

    private void vibrate() {
        VibratorAgent vibratorAgent = new VibratorAgent();
        List<Integer> vids = vibratorAgent.getVibratorIdList();
        LogPrintUtils.info("vibrate size" ,""+ vids.size());
        for (Integer position : vids) {
            LogPrintUtils.info("vibrate id " ,""+  vids);
            if (vibratorAgent.isSupport(position)) {
                vibratorAgent.startOnce(position, 200);
                break;
            }
        }
    }



    private void showTips(Context context, String message) {
        getUITaskDispatcher().asyncDispatch(() -> {
            ToastDialog toastDialog = new ToastDialog(context);
            toastDialog.setOffset(0, 100);
            toastDialog.setContentText(message);
            toastDialog.show();
        });
    }

    void setTitle(String title) {
        String res = "未识别";
        if (!StringUtils.isEmpty(title)) {
            res = title;
        }
        showTips(getContext(), res);
    }

    @Override
    public void getResult(String result) {
        if (StringUtils.isEmpty(result)) {
            return;
        }

        setTitle("扫描结果为：" + result);
        vibrate();
    }
}
